home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3812 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  74 lines

  1. Newsgroups: comp.lang.c
  2. Path: lyra.csx.cam.ac.uk!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: division problem 
  5. Message-ID: <DM1KMy.G3p@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <31097D77.11AA@rain.org> <26JAN199622082450@erich.triumf.ca> <4eh246$u6h@airdmhor.gen.nz> <4ej4ha$66@fountain.mindlink.net> <DLzvGG.2rn@uns.bris.ac.uk>  <Pine.SOL.3.90.960130150923.21923F-100000@flute>
  11. Date: Wed, 31 Jan 1996 10:47:22 GMT
  12.  
  13. Darrell Grainger (a378grai@cdf.toronto.edu) wrote:
  14. : On Tue, 30 Jan 1996, Nathan Sidwell (me) wrote:
  15.  
  16. : > Still no need to use floating point,
  17. : > celcius = ((fahrenheit - 32) * 5 + 4) / 9
  18.  
  19. : This ALMOST does the trick but 4/9 = 0.44444444... and 5/9 = 0.555555....
  20. : What you really want to do to eliminate the rounding errors is add 0.5 to
  21. : the number, e.g. 1.49999... + 0.5 = 1.9999... and (int)1.9999... = 1. So
  22. : 1.49999... rounds down to 1. Then 1.5 + 0.5 = 2.0 and (int)2.0 = 2. So
  23. : 1.5 rounds up to 2. This is what most people consider correct rounding.
  24. Integers and floating point numbers are not real numbers. This effects
  25. what 'correct' rounding is.
  26.  
  27. In this case the middle values are 4/9 and 5/9. We don't need to consider
  28. the case 4.5/9, which is what you're trying to do.
  29.  
  30. : In your 'trick' you would have 1.5 + 0.4444... = 1.9444... and
  31. : (int)1.9444... = 1. This means that 1.5 rounds down to 1. This is not
  32. As I've just said, this case NEVER HAPPENS.
  33.  
  34. : proper. It is close but not proper. Alternatively, adding 5/9th would round
  35. : up certain numbers it should not.
  36. adding 5/9 would round up the 4/9, which is wrong.
  37.  
  38. You are correct in pointing out that 4 ALMOST does the trick, but incorrect
  39. in your analysis.
  40.  
  41. Adding 4 will give the correct rounding for values >= 0 or if division
  42. truncates to most neg int.
  43. Adding 5 will  give incorrect rounding, whatever the domain of the dividend.
  44. Subtracting 4 gives correct rounding for values < 0 if division rounds
  45. towards zero.
  46.  
  47. What adding 4 is doing is moving the desired dividend values into the next
  48. integral multiple of 9. In this case the possible remainders are integers
  49. in the range [0,9)% (assuming +ve dividend or division rounding to most neg).
  50. We want remainders [0,5) to round down and [5,9) to round up.
  51. Adding 4 will move the these two ranges to [4,9) and [9,13), which
  52. indeed have the desired rounding behaviour.
  53.  
  54. For -ve dividend and division rounding to zero, the remainders are in the
  55. range (-9,0]. (-9,-5] wants to round down and (-5,0] round up. So we need to
  56. subtract 4, to move to the ranges (-13,-9] and (-9,-4].
  57.  
  58. For systems where division rounds towards zero, the expression should be
  59.  
  60. celcius = ((fahrenheit - 32) * 5 + (fahrenheit < 32 ? -4 : 4)) / 9
  61.  
  62. Depending on the target architecture and the strength of the compiler,
  63. the conditional expression could be evaluated with bit masking operations.
  64.  
  65. nathan
  66.  
  67. % for those unfamiliar, the notation [x,y) indicates the interval
  68. of values from x (inclusive) up to, but not including, y.
  69. --
  70. Nathan Sidwell                         Holder of the Xmris home page
  71. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  72. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  73. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  74.